#include "Ui.h"
#include "Scene.h"
#include "Input.h"

namespace Scene
{
	void transparentimage(IMAGE *dstimg, int x, int y, IMAGE *srcimg);
}

namespace Ui
{
	#define BUTTON_LAYOUT_COL  2

	#define BUTTON_WIDTH  176
	#define BUTTON_HEIGHT 60

	#define BUTTON_X_GAP 5
	#define BUTTON_Y_GAP 5

	#define FIRST_BUTTON_XPOS 420
	#define FIRST_BUTTON_YPOS 500


	std::shared_ptr<Button> buttons[5];

	Button::Button(int index)
	{
		m_Index = index;
		m_IsPress = 0;

		m_Height = BUTTON_HEIGHT;
		m_Width = BUTTON_WIDTH;

		m_Xpos = FIRST_BUTTON_XPOS + (index % BUTTON_LAYOUT_COL) * (BUTTON_WIDTH  + BUTTON_X_GAP);
		m_Ypos = FIRST_BUTTON_YPOS + (index / BUTTON_LAYOUT_COL) * (BUTTON_HEIGHT + BUTTON_Y_GAP);

		switch (index)
		{
		case 1: OnHit = []() { Input::Random(0); };	 break;
		case 3: OnHit = []() { Input::Random(1); };	 break;
		case 0: OnHit = []() { Input::GetData(0); }; break;
		case 2: OnHit = []() { Input::GetData(1); }; break;
		case 4: OnHit = []() { Input::Checker(); };  break;
		default:
			break;
		}

		m_Sprite0.reset(new IMAGE);
		m_Sprite1.reset(new IMAGE);
		std::string filename0 = "res/button/" + std::to_string(index) + "0.png";
		std::string filename1 = "res/button/" + std::to_string(index) + "1.png";
		loadimage(m_Sprite0.get(), filename0.c_str(), m_Width, m_Height);
		loadimage(m_Sprite1.get(), filename1.c_str(), m_Width, m_Height);

		putimage(m_Xpos, m_Ypos, m_Sprite0.get());
		//Scene::transparentimage(NULL, m_Xpos, m_Ypos, m_Sprite0.get());
	}

	void Button::IsHit(int x, int y, int release)
	{
		if (x > m_Xpos && x < m_Xpos + m_Width && y > m_Ypos && y < m_Ypos + m_Height) {
			if (release && m_IsPress) {
				putimage(m_Xpos, m_Ypos, m_Sprite0.get());
				OnHit();
			}
			else {
				putimage(m_Xpos, m_Ypos, m_Sprite1.get());
				m_IsPress = 1;
			}
		}
		else {
			putimage(m_Xpos, m_Ypos, m_Sprite0.get());
			m_IsPress = 0;
		}
	}

	void CreateUi()
	{
		for (int i = 0; i < 5; i++) {
			buttons[i].reset(new Button(i));
		}
	}
}
